package app.controller.city;

import app.entity.UserSession;
import app.service.DeviceManageService;
import app.service.FacilitiesManageService;
import app.utils.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import common.util.RegionUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import refactor.entity.FacMonitoring;
import refactor.service.IFacMonitoringService;
import siotwx.entity.Countsamefacaddress;
import siotwx.entity.Facmonitoring;
import siotwx.entity.Module;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 百度地图相关的控制器
 *
 * @author 昊琦
 * 创建时间：2018年3月15日11:11:34
 *
 */
@Controller
public class BaiduMapController {

	private static final Log logger = LogFactory.getLog(BaiduMapController.class);
	
	@Autowired
	@Qualifier("FacilitiesManageService")
	private FacilitiesManageService fms;
	
	@Autowired
	@Qualifier("DeviceManageService")
	private DeviceManageService dms;

	private IFacMonitoringService facMonitoringService;

	@Autowired
	public void setFacMonitoringService(IFacMonitoringService facMonitoringService) {
		this.facMonitoringService = facMonitoringService;
	}

	@RequestMapping(value="/ajaxpoisearch",method= RequestMethod.POST)
	public void baiduPOIsearch(String name,String lng,String lat,HttpServletResponse response) throws Exception {
		//标准化收到信息
		name = name.replaceAll(" ", "");
		lng = lng.replaceAll(" ", "");
		lat = lat.replaceAll(" ", "");
		List<BaiduMapAddressBean> posList = new BaiduMapUtils().POISearch(name, lng, lat, 700);
		DataResponse.jsonResponse(response, posList);
		return;
	}
	
	/**
	 * 对应首页的统计图的地图
	 * @param devMod
	 * @param request
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(value="2/indexdevicemap")
	public String indexCityMap(@RequestParam("devMod")String devMod,HttpServletRequest request,HttpSession session,Model model){
		
	
		
		//获取用户regionId
		UserSession userSession = (UserSession)session.getAttribute(Common.sessionUser);
		int regionId = userSession.getRegionId();
		
		logger.info("BaiduMapController-indexCityMap()执行开始");
		
		model.addAttribute("basePath", BasePathCollector.getBasePathWithoutPort(request));
		
		//获取设备类型码
		Module module = dms.getModule(devMod);
		String modType = "";
		if (module!=null) {
			modType=module.getCode();
			
			//获取该地区的所有故障设施（包括绑在设施上的设备）
			List<Facmonitoring> list = fms.getFacWarningData(modType, regionId);
			
			//把list填入model
			model.addAttribute("list", list);
			
			if (list!=null&&!list.isEmpty()) {
				String longitude = list.get(0).getFacLon();
				String latitude = list.get(0).getFacLat();
				
				//把经纬度填入model
				model.addAttribute("longitude", longitude);
				model.addAttribute("latitude", latitude);
			}
			
		}
		
		logger.info("BaiduMapController-indexCityMap()执行结束");
		
		return "city/indexdevicemap";
		
	}

	@GetMapping("/2/location")
	public String deviceLocation(String facId, Model model, HttpServletRequest request) {

		QueryWrapper<FacMonitoring> facmonitoringQueryWrapper = new QueryWrapper<>();
		facmonitoringQueryWrapper.eq("facId", facId);

		FacMonitoring facmonitoring = facMonitoringService.getOne(facmonitoringQueryWrapper);

		model.addAttribute("facId", facId);
		model.addAttribute("facDesc", facmonitoring.getFacDesc());
		model.addAttribute("latitude", facmonitoring.getFacLat());
		model.addAttribute("longitude", facmonitoring.getFacLon());
		model.addAttribute("devCode", facmonitoring.getDevCode());
		model.addAttribute("eventDevstu", facmonitoring.getEventDevStu());
		model.addAttribute("eventDoorstu", facmonitoring.getEventDoorStu());

		//获取用户信息
		Integer regionId = RegionUtil.userRegion(request.getSession());

		logger.info("BaiduMapController-showDeviceMap()执行开始");

		Map<String, Object> param = new HashMap<>();
		param.put("facRegionId", regionId);

		List<Facmonitoring> showlist = fms.getFacmonitoring(regionId);

		model.addAttribute("showlist", showlist);

		logger.info("BaiduMapController-showDeviceMap()执行结束");

		return "city/devicemap";
	}
	
	
	/**
	 * 在设施实时监测界面通过点击显示地图
	 * @param facId
	 * @param facDesc
	 * @param latitude
	 * @param longitude
	 * @param devCode
	 * @param eventDevstu
	 * @param eventDoorstu
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping("2/devicemap")
	public String showDeviceMap(String facId,String facDesc,String latitude,
			String longitude,String devCode,String eventDevstu,String eventDoorstu,HttpServletRequest request,HttpSession session,Model model){
		
		
		
		model.addAttribute("facId", facId);
		model.addAttribute("facDesc", facDesc);
		model.addAttribute("latitude", latitude);
		model.addAttribute("longitude", longitude);
		model.addAttribute("devCode", devCode);
		model.addAttribute("eventDevstu", eventDevstu);
		model.addAttribute("eventDoorstu", eventDoorstu);
		
		//获取用户信息
		UserSession userSession = (UserSession)session.getAttribute(Common.sessionUser);
		int regionId = userSession.getRegionId();
		
		logger.info("BaiduMapController-showDeviceMap()执行开始");
		
		Map<String, Object> param = new HashMap<>();
		param.put("facRegionId", regionId);
		
		List<Facmonitoring> showlist = fms.getFacmonitoring(regionId);
		
		model.addAttribute("showlist", showlist);
		
		logger.info("BaiduMapController-showDeviceMap()执行结束");
		
		return "city/devicemap";
	}
	
	/**
	 * 获取在相同的坐标上安装了多少个设备
	 * @param response
	 * @param session
	 */
	@RequestMapping("ajaxgetsamefacaddress")
	public void getSameFacAddress(HttpServletResponse response,HttpSession session){
		

		
		//获取用户地区
		
		UserSession userSession = (UserSession)session.getAttribute(Common.sessionUser);
		
		int regionId = userSession.getRegionId();
		
		logger.info("BaiduMapController-getSameFacAddress()执行开始");
		
		List<Countsamefacaddress> list = dms.getSameFacAddress(regionId);
		
		logger.info("BaiduMapController-getSameFacAddress()执行结束");
		
		DataResponse.jsonResponse(response, list);
		
	}
}
